﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

using LowLevelGraphics;
using LowLevelGraphics.Filter;
//using LowLevelGraphics.Extractors;

namespace ImageRecognition2.Action
{
    /// <summary>
    /// Use this to extract a blob
    /// </summary>
    public class ActionColorBlobExtractor : AbstractCommand
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ActionColorBlobExtractor"/> class.
        /// </summary>
        public ActionColorBlobExtractor()
        {
        }

        /// <summary>
        /// Runs the specified sender.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        public override void Run(object sender, EventArgs e)
        {
            Bitmap bitmap = (Bitmap)MainFormSingleton.ActivePictureBox.BackgroundImage;
            List<ColorRect> m_aColorRect = new ColorBlobExtractor(bitmap, new Size(15, 15)).ExtractBlobs(10, 10, 10);

            m_aColorRect.Sort(new ColorRectSort());

            Graphics g = Graphics.FromImage(bitmap);

            foreach (ColorRect rect in m_aColorRect)
            {
                using (Pen pen = new Pen(new SolidBrush(rect.Color)))
                {
                    g.DrawRectangle(pen, rect.Rectangle);
                }
            }

            MainFormSingleton.ActivePictureBox.Invalidate();
        }
    }
}
